<?php


namespace App\Http\Responses;

use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\Response as Res;


class Response
{
    /* private make response method */
    private static function __makeResponse($responseCode): JsonResponse
    {
        return Res::json([
            "code" => $responseCode["code"],
            "message" => $responseCode["message"]
        ]);
    }

    private static function __makeResourceResponse($responseCode, $data): JsonResponse
    {
        return Res::json([
            "code" => $responseCode["code"],
            "message" => $responseCode["message"],
            "data" => [
                "count" => count($data),
                "content" => $data
            ]
        ]);
    }

    private static function __makeSingleResourceResponse($responseCode, $data): JsonResponse
    {
        return Res::json([
            "code" => $responseCode["code"],
            "message" => $responseCode["message"],
            "data" => $data
        ]);
    }


    /* success result method */
    public static function success($responseCode = ResponseCode::SUCCESS): JsonResponse
    {
        return self::__makeResponse($responseCode);
    }

    public static function select_one($data, $responseCode = ResponseCode::SUCCESS): JsonResponse
    {
        return self::__makeSingleResourceResponse($responseCode, $data);
    }

    public static function insert_success($data = null, $responseCode = ResponseCode::INSERT_SUCCESS): JsonResponse
    {
        return self::__makeSingleResourceResponse($responseCode, $data);
    }

    public static function update_success($data = null, $responseCode = ResponseCode::UPDATE_SUCCESS): JsonResponse
    {
        return self::__makeSingleResourceResponse($responseCode, $data);
    }

    public static function delete_success($data = null, $responseCode = ResponseCode::DELETE_SUCCESS): JsonResponse
    {
        return self::__makeSingleResourceResponse($responseCode, $data);
    }


    /* error result method */
    public static function not_found($responseCode = ResponseCode::NOT_FOUND): JsonResponse
    {
        return self::__makeResponse($responseCode);
    }

    public static function insert_error($responseCode = ResponseCode::INSERT_ERROR): JsonResponse
    {
        return self::__makeResponse($responseCode);
    }

    public static function update_error($responseCode = ResponseCode::UPDATE_ERROR): JsonResponse
    {
        return self::__makeResponse($responseCode);
    }

    public static function delete_error($responseCode = ResponseCode::DELETE_ERROR): JsonResponse
    {
        return self::__makeResponse($responseCode);
    }

    public static function resource_success($data = [], $responseCode = ResponseCode::SUCCESS): JsonResponse
    {
        return self::__makeResourceResponse($responseCode, $data);
    }

}
